﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Office = Microsoft.Office.Core;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Windows.Forms;
using System.Reflection;
using System.Runtime.InteropServices;
using System.IO;

namespace AutoEmailAddIn
{
    partial class AutoEmailForm
    {
        #region Form Region Factory

        [Microsoft.Office.Tools.Outlook.FormRegionMessageClass(Microsoft.Office.Tools.Outlook.FormRegionMessageClassAttribute.Appointment)]
        [Microsoft.Office.Tools.Outlook.FormRegionMessageClass(Microsoft.Office.Tools.Outlook.FormRegionMessageClassAttribute.Contact)]
        [Microsoft.Office.Tools.Outlook.FormRegionMessageClass(Microsoft.Office.Tools.Outlook.FormRegionMessageClassAttribute.Note)]
        [Microsoft.Office.Tools.Outlook.FormRegionMessageClass(Microsoft.Office.Tools.Outlook.FormRegionMessageClassAttribute.DistributionList)]
        [Microsoft.Office.Tools.Outlook.FormRegionName("AutoEmailAddIn.AutoEmailForm")]
        public partial class AutoEmailFormFactory
        {
            // Occurs before the form region is initialized.
            // To prevent the form region from appearing, set e.Cancel to true.
            // Use e.OutlookItem to get a reference to the current Outlook item.
            private void AutoEmailFormFactory_FormRegionInitializing(object sender, Microsoft.Office.Tools.Outlook.FormRegionInitializingEventArgs e)
            {
            }
        }

        #endregion

        private const string LogPath = @"c:\AudoEmailAddIn.log";
        private StreamWriter log = new StreamWriter(LogPath);

        private bool m_bEnabledState = true;
        private System.DateTime m_dtWorkStartTime = new System.DateTime(); // start time worktime
        private System.DateTime m_dtStartComputer = new System.DateTime(); //start computer time

        // Occurs before the form region is displayed.
        // Use this.OutlookItem to get a reference to the current Outlook item.
        // Use this.OutlookFormRegion to get a reference to the form region.
        private void AutoEmailForm_FormRegionShowing(object sender, System.EventArgs e)
        {
        }

        // Occurs when the form region is closed.
        // Use this.OutlookItem to get a reference to the current Outlook item.
        // Use this.OutlookFormRegion to get a reference to the form region.
        private void AutoEmailForm_FormRegionClosed(object sender, System.EventArgs e)
        {
        }

        private void hook_MainKeyDown(object sender, KeyEventArgs e)
        {
            this.m_dtStartComputer = DateTime.Now;
            hook_Main.UnInstallHook();
        }

        private void hook_MainKeyPress(object sender, KeyPressEventArgs e)
        {
            this.m_dtStartComputer = DateTime.Now;
            hook_Main.UnInstallHook();
        }

        private void hook_MainKeyUp(object sender, KeyEventArgs e)
        {
            this.m_dtStartComputer = DateTime.Now;
            hook_Main.UnInstallHook();
        }

        private void hook_MainMouseMove(object sender, MouseEventArgs e)
        {
            this.m_dtStartComputer = DateTime.Now;
            hook_Main.UnInstallHook();
        }

        private void lbEnabled_Click(object sender, EventArgs e)
        {
            this.m_bEnabledState = m_bEnabledState == true ? false : true;
            if (this.m_bEnabledState)
            {
                this.lbEnabled.Text = "禁用";
                this.tmWork.Start();
            }
            else
            {
                this.lbEnabled.Text = "启用";
                this.tmWork.Stop();
            }
        }

        private static void sendEmail(string strSendTo,string strSmtp,string strMailTitle,string strMailBody)
        {
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
            message.To.Add(strSendTo);
            message.Subject = strMailTitle;
            message.From = new System.Net.Mail.MailAddress(strSendTo);
            message.Body = strMailBody;
            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(strSmtp);
            smtp.Send(message);
        }

        private System.DateTime getStartComputerTime()
        {
            return DateTime.Now.AddMilliseconds(0 - Environment.TickCount);

            /*int hm = System.Environment.TickCount;
            double h = hm / 3600000.0;
            double m = double.Parse("0." + h.ToString("F6").Substring(h.ToString().IndexOf(".") + 1)) * 60;
            double s = double.Parse("0." + m.ToString("F6").Substring(m.ToString().IndexOf(".") + 1)) * 60;
            string h1 = ((int)h).ToString();
            string m1 = ((int)m).ToString();
            string s1 = ((int)s).ToString();
            if ((int)h < 10) h1 = "0" + h1;
            if ((int)m < 10) m1 = "0" + m1;
            if ((int)s < 10) s1 = "0" + s1;
            string nowtime = DateTime.Now.TimeOfDay.ToString().Substring(0, 8);
            string span = h1 + ":" + m1 + ":" + s1;
            TimeSpan spantime = Convert.ToDateTime(nowtime).Subtract(Convert.ToDateTime(span));
            System.Windows.Forms.MessageBox.Show("您的开机时间是： " + spantime.ToString());*/
        }

        private string getMailBody()
        {
            string strMailBody = "上班时间：" + this.m_dtStartComputer.ToString() 
                + "\n当前时间：" + DateTime.Now.ToString() 
                + "\n上班时长：" + (DateTime.Now - this.m_dtStartComputer).ToString();
            return strMailBody;
        }

        private void tmWorkTimeout(object sender, EventArgs e)
        {
            log.WriteLine("WorkStartTime:" + this.m_dtWorkStartTime.ToString() + "Now: " + DateTime.Now.ToString());

            if (this.m_dtWorkStartTime.Day < DateTime.Now.Day)
            {
                this.m_dtWorkStartTime = DateTime.Now;
                hook_Main.InstallHook("1");
            }

            if (this.m_dtStartComputer.AddMilliseconds(getWorkDurationMs()) < DateTime.Now)
            {
                sendEmail(this.tbMailToWho.Text, this.tbSmtp.Text, this.tbMailTitle.Text, getMailBody());
                return;
            }    
        }

        private int getWorkDurationMs()
        {
            int iWorkDurationHour = int.Parse(this.tbWorkDurationHour.Text);
            int iWorkDurationMin = int.Parse(this.tbWorkDurationMin.Text);

            return (iWorkDurationHour*60*60 + iWorkDurationMin*60)*1000;
        }
       
    }

}
